home *** CD-ROM | disk | FTP | other *** search
- #include <Power.h>
- #include <QDOffscreen.h>
- #include <ControlDefinitions.h>
- #include <ControlStrip.h>
- #include "Light Sleeper.h"
- #include "Backlight.h"
-
- #define kPauseLength 30
- #define kMovementWindow 2
-
- long MyDrinkJolt( long message, SleepQRecPtr qRecPtr );
-
- UniversalProcPtr gOldGetNextEvent = nil, gNewGetNextEvent;
- UniversalProcPtr gOldFrontWindow = nil, gNewFrontWindow;
- UniversalProcPtr gOldButton = nil, gNewButton;
- WindowPtr myFrontWindow = nil;
- Boolean selectWindow = true, lockButton = false, controlStripAvail = false;
- Point mouseLoc;
- short theRatio = 1;
- long ticks = 0, trash;
-
- ControlHandle theScrollBar;
-
- void main( void )
- {
- SleepQRec mySleepRec;
-
- if ( Gestalt( gestaltControlStripVersion, &trash ) == noErr )
- controlStripAvail = true;
-
- InitializeToolbox();
-
- mySleepRec.sleepQType = slpQType;
- mySleepRec.sleepQProc = NewSleepQUPP( MyDrinkJolt );
- SleepQInstall( &mySleepRec );
-
- DrawMenubar();
-
- do
- {
- EventOnce();
- }
- while ( done == false );
-
- SleepQRemove( &mySleepRec );
- }
-
- void InitializeToolbox( void )
- {
- InitGraf( &qd.thePort );
- InitFonts();
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs( 0L );
- FlushEvents( everyEvent, 0 );
- InitCursor();
- }
-
- void DrawMenubar( void )
- {
- Handle menuBar;
- MenuHandle menu;
-
- menuBar= GetNewMBar( rMenuBar );
- SetMenuBar( menuBar );
- DrawMenuBar();
-
- menu = GetMenuHandle( mApple );
- AppendResMenu( menu, 'DRVR' );
- }
-
- void EventOnce( void )
- {
- EventRecord event;
-
- WaitNextEvent( everyEvent, &event, 0x7FFFFFFF, nil );
-
- switch ( event.what )
- {
- case mouseDown:
- HandleMouseDown( &event );
- break;
- case keyDown:
- HandleKeyDown( event );
- break;
- case autoKey:
- HandleKeyDown( event );
- break;
- case updateEvt:
- HandleUpdate( event );
- break;
- case activateEvt:
- break;
- }
- }
-
- void HandleUpdate( EventRecord theEvent )
- {
- WindowPtr window;
-
- window = (WindowPtr)theEvent.message;
-
- BeginUpdate( window );
- SetPort( window );
- DrawControls( window );
- EndUpdate( window );
- }
-
- void HandleMouseDown( EventRecord *eventPtr )
- {
- WindowPtr whichWindow, savePort;
- Rect dragRect = {0,0,3000, 3000};
- Point thePoint;
- ControlHandle theControl;
- short thePart;
- long menuChoice;
-
- thePart = FindWindow( eventPtr->where, &whichWindow );
- thePoint = (*eventPtr).where;
- switch (thePart)
- {
- case inSysWindow:
- SystemClick( eventPtr, whichWindow );
- break;
- case inMenuBar:
- menuChoice = MenuSelect( eventPtr->where );
- if ( menuChoice != 0 )
- HandleMenuChoice( menuChoice );
- case inDrag:
- DragWindow( whichWindow, eventPtr->where, &dragRect);
- break;
- case inGoAway:
- if ( TrackGoAway(whichWindow, eventPtr->where) == true)
- DisposeWindow(whichWindow);
- break;
- case inContent:
- if ( whichWindow != FrontWindow() )
- {
- SelectWindow( whichWindow );
- return;
- }
-
- GetPort( &savePort );
- SetPort( whichWindow );
- GlobalToLocal( &thePoint );
- SetPort( savePort );
- theControl = FindControlUnderMouse( thePoint, whichWindow, &thePart );
- thePart = HandleControlClick( theControl, thePoint, (*eventPtr).modifiers, (ControlActionUPP)-1L );
-
- break;
- }
- HiliteMenu( 0 );
- }
-
- void HandleAppleChoice( short item )
- {
- MenuHandle appleMenu;
- Str255 accName;
- short accNumber;
-
- switch ( item )
- {
- case iAbout:
- AboutDialog();
- break;
- default:
- appleMenu = GetMenuHandle( mApple );
- GetMenuItemText( appleMenu, item, accName );
- accNumber = OpenDeskAcc( accName );
- break;
- }
- }
-
- void HandleFileChoice( short item )
- {
- switch ( item )
- {
- case iQuit:
- done = true;
- break;
- }
- }
-
- void HandleKeyDown( EventRecord theEvent )
- {
- short theChar;
- long theChoice;
-
- theChar = theEvent.message & charCodeMask;
-
- if ( ( theEvent.modifiers & cmdKey ) != 0 )
- {
- if ( theEvent.what != autoKey )
- {
- theChoice = MenuKey( theChar );
- HandleMenuChoice( theChoice );
- }
- }
- }
-
- void HandleMenuChoice( long theChoice )
- {
- short theMenu;
- short theMenuItem;
-
- theMenu = HiWord( theChoice );
- theMenuItem = LoWord( theChoice );
- switch ( theMenu )
- {
- case mApple:
- HandleAppleChoice( theMenuItem );
- break;
- case mFile:
- HandleFileChoice( theMenuItem );
- break;
- }
- HiliteMenu( 0 );
- }
-
- void AboutDialog( void )
- {
- GrafPtr savePort;
- DialogPtr dlog;
- short item;
-
- GetPort( &savePort );
-
- dlog = GetNewDialog( 128, 0L, (WindowPtr) -1L );
-
- SetDialogDefaultItem( dlog, 1 );
- SetPort( dlog );
-
- if ( dlog )
- {
- do
- {
- ModalDialog( nil, &item );
- }
- while ( item != 1 );
-
- DisposeDialog(dlog);
- }
-
- SetPort( savePort );
- }
-
- long MyDrinkJolt( long message, SleepQRecPtr qRecPtr )
- {
- #pragma unused ( qRecPtr )
-
- WindowPtr theWindow;
- Boolean controlStripVis;
- short speed = 1;
- int ii;
- long tickCount;
- unsigned long finalTicks;
-
- if ( message != sleepRequest && message != sleepDemand )
- return 0;
-
- InitializeBacklightControl();
-
- if ( controlStripAvail )
- {
- controlStripVis = SBIsControlStripVisible();
- SBShowHideControlStrip( false );
- }
- HideMenuBar();
- HideCursor();
-
- theWindow = GetNewCWindow( 128, nil, (WindowPtr)-1 );
- SizeWindow( theWindow, qd.screenBits.bounds.right, qd.screenBits.bounds.bottom, true );
-
- do
- {
- if(speed < 1)
- speed = 1;
-
- for(ii = 0; ii <= 60; ii += speed)
- {
- tickCount = LMGetTicks();
-
- Delay( 2, &finalTicks );
-
- if(ii > 60)
- ii = 60;
- SetBacklightBrightness(31 - ii/2);
- while(LMGetTicks() == tickCount) {};
- }
-
- if(speed < 1)
- speed = 1;
-
- for(ii = 0; ii < 60 + speed; ii += speed)
- {
- tickCount = LMGetTicks();
-
- Delay( 2, &finalTicks );
-
- if(ii > 60)
- ii = 60;
- SetBacklightBrightness(2 + ii/2);
- while(LMGetTicks() == tickCount) {};
- }
- }
- while( !Button() );
-
- DisposeWindow( theWindow );
-
- if ( controlStripAvail )
- {
- SBShowHideControlStrip( controlStripVis );
- }
-
- ShowMenuBar();
- InitCursor();
-
- return 1;
- }